﻿Imports System.Data.OleDb
Imports BUS
Imports DTO

Public Class frmBanVe

    Private Sub frmBanVe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbxTuyenXe.DataSource = TuyenBus.LayDanhSachTuyen()
        cbxTuyenXe.DisplayMember = "TenTuyen"
        cbxTuyenXe.ValueMember = "MaTuyen"
    End Sub

    Private Sub cbxTuyenXe_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxTuyenXe.SelectedIndexChanged
        cbxNgayKhoiHanh.Text = ""
        cbxTuyenXe.ValueMember = "MaTuyen"
        Dim maTuyen As Long = cbxTuyenXe.SelectedValue
        cbxNgayKhoiHanh.DataSource = LichGanBus.TimNgayKhoiHanh(maTuyen)
        cbxNgayKhoiHanh.DisplayMember = "NgayKhoiHanh"
    End Sub

    Private Sub cbxNgayKhoiHanh_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxNgayKhoiHanh.SelectedIndexChanged, cbxNgayKhoiHanh.TextChanged
        cbxGioKhoiHanh.Text = ""
        cbxTuyenXe.ValueMember = "MaTuyen"
        cbxNgayKhoiHanh.ValueMember = "NgayKhoiHanh"

        Dim maTuyen As Long = cbxTuyenXe.SelectedValue
        Dim ngayKH As Date = cbxNgayKhoiHanh.SelectedValue
        cbxGioKhoiHanh.DataSource = LichGanBus.TimThoiDiemKhoiHanh(maTuyen, ngayKH)
        cbxGioKhoiHanh.DisplayMember = "ThoiDiemKhoiHanh"
        cbxGioKhoiHanh.ValueMember = "ThoiDiemKhoiHanh"
    End Sub

    Private Sub cbxGioKhoiHanh_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxGioKhoiHanh.SelectedIndexChanged, cbxGioKhoiHanh.TextChanged
        cbxTuyenXe.ValueMember = "MaTuyen"
        cbxNgayKhoiHanh.ValueMember = "NgayKhoiHanh"
        cbxGioKhoiHanh.ValueMember = "ThoiDiemKhoiHanh"

        Dim maTuyen As Long = cbxTuyenXe.SelectedValue
        Dim ngayKH As Date = cbxNgayKhoiHanh.SelectedValue
        Dim thoiDiemKH As String = cbxGioKhoiHanh.SelectedValue

        dgvXe.DataSource = LichGanBus.DanhSachXe_TheoTuyen_ThoiDiem(maTuyen, ngayKH, thoiDiemKH)
    End Sub

    Private Sub HienThiVe()
        cbxTuyenXe.ValueMember = "MaTuyen"
        cbxNgayKhoiHanh.ValueMember = "NgayKhoiHanh"
        cbxGioKhoiHanh.ValueMember = "ThoiDiemKhoiHanh"

        Dim maTuyen As Long = cbxTuyenXe.SelectedValue
        Dim ngayKH As Date = cbxNgayKhoiHanh.SelectedValue
        Dim thoiDiemKH As String = cbxGioKhoiHanh.SelectedValue
        Dim vitri, MaXe As Integer
        Try
            vitri = dgvXe.CurrentRow.Index

            MaXe = dgvXe.Rows(vitri).Cells("MaXe").Value
        Catch ex As Exception
        End Try
        Dim maLichGan As Long
        Try
            Dim dt As DataTable = LichGanBus.TimLichGan(maTuyen, ngayKH, thoiDiemKH, MaXe)
            maLichGan = dt.Rows(0).Item("MaLichGan")
        Catch ex As Exception
            '.......
        End Try
        dgvVe.DataSource = VeBus.TimVeTheoMaLichGan(maLichGan)
    End Sub

    Private Sub dgvXe_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvXe.MouseClick
        HienThiVe()
    End Sub

    Private Sub dgvXe_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvXe.DataSourceChanged
        HienThiVe()
    End Sub

    Dim vitriClick As Integer
    Private Sub dgvVe_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvVe.MouseClick
        Try
            vitriClick = dgvVe.CurrentRow.Index
        Catch ex As Exception
        End Try
    End Sub

    Dim ve As New VeDto

    Private Sub btnBanVe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBanVe.Click
        Dim maGhe As Integer
        Dim maLichGan As Long
        Dim tinhTrang As Boolean

        If vitriClick = dgvVe.Rows.Count - 1 Then
            MessageBox.Show("Chọn dòng có dữ liệu!!!", "THÔNG BÁO")
            Exit Sub
        End If
        Try
            maGhe = dgvVe.Rows(vitriClick).Cells("MaGhe").Value
            maLichGan = dgvVe.Rows(vitriClick).Cells("MaLich").Value
            tinhTrang = dgvVe.Rows(vitriClick).Cells("TinhTrang").Value
        Catch ex As Exception
            MessageBox.Show("Chọn một chỗ ngồi (mã ghế)!", "THÔNG BÁO")
            Exit Sub
        End Try

        If tinhTrang Then
            MessageBox.Show("Vé này đã bán!", "THÔNG BÁO")
            Exit Sub
        End If
        ve.MaGhe = maGhe
        ve.MaLich = maLichGan
        ve.TinhTrang = True
        'dgvVe.Columns("MaLoaiVe").Visible = False
        ve.LoaiVe = dgvVe.Rows(vitriClick).Cells("MaLoaiVe").Value
        VeBus.CapNhatVe(ve)
        HienThiVe()
        dgvVe.Rows(0).Selected = False
        dgvVe.Rows(vitriClick).Selected = True
        btnHuy.Enabled = True
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        ve.TinhTrang = False
        VeBus.CapNhatVe(ve)
        HienThiVe()
        dgvVe.Rows(0).Selected = False
        dgvVe.Rows(vitriClick).Selected = True
        btnHuy.Enabled = False
    End Sub

    Private Sub btnDong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Close()
    End Sub


End Class